# coding: utf-8 
"""
@Time    : 2024/8/27 14:00
@Author  : Y.H LEE
"""
import numpy as np
import matplotlib.pyplot as plt

from benchmarks.test_algorithms.genetic_algorithm import GeneticAlgorithm

"""
The TSP (TravelSalesPerson) Question as Benchmark
"""

# hyper_params settings
N_CITIES = 28  # == DNA size
CROSS_RATE = 0.1  # 交叉概率
MUTATE_RATE = 0.04  # 变异概率
POP_SIZE = 5000  # 个体数目
N_GENERATIONS = 1000  # 繁衍代数


class TravelSalesPerson(object):
    def __init__(self, n_cities, city_position=None, interactive=True):
        """
        初始化旅行城市坐标，使用TkAgg作为后端，打开交互式show (plt.ion)
        :param n_cities: DNA_size
        :param city_position: city location
        :param interactive:
        """
        self.city_position = city_position if city_position is not None else np.random.rand(n_cities, 2)
        if interactive:
            plt.switch_backend('TkAgg')
            plt.ion()

    def plotting(self, lx, ly, total_d):
        # lx, ly = np.append(lx, lx[0]), np.append(ly, ly[0])
        plt.cla()
        # 画城市所在的点
        plt.scatter(self.city_position[:, 0].T, self.city_position[:, 1].T, s=50, c='k')
        # 画路线
        plt.plot(lx.T, ly.T, 'r-')
        plt.text(-0.05, -0.09, "Total distance=%.2f" % total_d, fontdict={'size': 16, 'color': 'black'})
        plt.xlim((-0.1, 1.1))
        plt.ylim((-0.1, 1.1))
        plt.pause(0.01)


if __name__ == '__main__':
    test_case_01 = np.array([[0.30914913, 0.39293522],
                             [0.54713058, 0.7132294],
                             [0.12242006, 0.27167182],
                             [0.37448828, 0.66368665],
                             [0.27583736, 0.59745111],
                             [0.37296817, 0.43128948],
                             [0.32799687, 0.84400197],
                             [0.89437572, 0.42122869],
                             [0.17814603, 0.79765564],
                             [0.46548399, 0.36688028],
                             [0.28140457, 0.31164782],
                             [0.22088643, 0.10025304],
                             [0.0374093, 0.01412182],
                             [0.04351789, 0.10887305],
                             [0.62701684, 0.60010578],
                             [0.60243429, 0.29535763],
                             [0.42648814, 0.10313667],
                             [0.0850267, 0.89213179],
                             [0.65216502, 0.82445387],
                             [0.89494803, 0.97114676],
                             [0.71569797, 0.94964098],
                             [0.87359296, 0.06994598],
                             [0.61242782, 0.29544201],
                             [0.26317581, 0.3740427],
                             [0.54257997, 0.74796963],
                             [0.80832047, 0.52528606],
                             [0.82442331, 0.00986019],
                             [0.74880775, 0.49968532]])
    # 实例化地图
    env = TravelSalesPerson(N_CITIES, city_position=test_case_01)

    """实例化对象"""
    test_obj = GeneticAlgorithm(DNA_size=N_CITIES, cross_rate=CROSS_RATE, mutation_rate=MUTATE_RATE, pop_size=POP_SIZE)

    """测试"""
    test_obj.start(env, N_GENERATIONS)
    plt.ioff()
    plt.show()

"""
test_01:
[[0.30914913 0.39293522]
 [0.54713058 0.7132294 ]
 [0.12242006 0.27167182]
 [0.37448828 0.66368665]
 [0.27583736 0.59745111]
 [0.37296817 0.43128948]
 [0.32799687 0.84400197]
 [0.89437572 0.42122869]
 [0.17814603 0.79765564]
 [0.46548399 0.36688028]
 [0.28140457 0.31164782]
 [0.22088643 0.10025304]
 [0.0374093  0.01412182]
 [0.04351789 0.10887305]
 [0.62701684 0.60010578]
 [0.60243429 0.29535763]
 [0.42648814 0.10313667]
 [0.0850267  0.89213179]
 [0.65216502 0.82445387]
 [0.89494803 0.97114676]
 [0.71569797 0.94964098]
 [0.87359296 0.06994598]
 [0.61242782 0.29544201]
 [0.26317581 0.3740427 ]
 [0.54257997 0.74796963]
 [0.80832047 0.52528606]
 [0.82442331 0.00986019]
 [0.74880775 0.49968532]]
 results:
    1. 5.46
    2. 5.47
    3. 6.20
    4. 5.91
    5. 5.23
    6. 5.19
    7. 4.71 (mutate==0.04, population==5000)

"""
